<?php


namespace app\index\controller;


use app\common\model\WebConfig;
use app\common\model\Comment as CommentModel;
use think\facade\Cookie;

class Comment extends Base
{
    /**
     * 发布评论
     */
    public function publish()
    {
        $waitSign = md5('wait_time_publish');
        $waitTime = Cookie::get($waitSign);
        $waitTimeResult = time() - $waitTime;
        if ($waitTimeResult <= 60) {
            $time = 60 - $waitTimeResult;
            $this->error('您提交的太频繁了，稍微缓缓。喝杯咖啡吧！' . $time . '秒后可重新提交');
        }
        $params = $this->requestParams();
        if (empty($params['user_id'])) {
            $params['user_id'] = -1;
        }
        $params['nick_name'] = $params['nick_name'] ?? '匿名';
        //干掉所有标签
        $params['nick_name'] = htmlentities(strip_tags($params['nick_name']));
        $params['content'] = htmlentities(strip_tags($params['content']));
        $params['ip'] = $this->request->ip();
        $webConfig = WebConfig::getWebConfig();
        $params['status'] = 0;//默认审核
        $msg = '您的评论将在审核成功后显示';
        if ($webConfig['verify_comment'] == 0) {
            $params['status'] = 1;
            $msg = '评论成功';
        }
        $result = CommentModel::create($params);
        $with = ['comments' => function ($query) {
            $order = ['create_time desc'];
            $query->order($order)->with(['repleyuser', 'user'])->withcount(['comments']);
        }, 'user'];
        $data = $result->where(['id' => $result->id])->with($with)->select();
        if ($result) {
            Cookie::set($waitSign, time());
            $data = ['list' => $data];
            $this->success($msg, '', $data);
        }
    }

    /**
     * 评论列表
     */
    public function list()
    {
        $params = $this->request->param();
        if (!isset($params['goal_type']) || empty($params['goal_type'])) {
            $this->error('参数异常');
        }
        if (empty($params['goal_id'])) {
            $this->error('目标内容错误');
        }
        $page = $params['page'] ?: 1;
        $goalId = $params['goal_id'];
        $goalType = $params['goal_type'];
        $limit = 5;

        $where = ['pid' => 0, 'status' => 1, 'goal_type' => $goalType, 'goal_id' => $goalId];
        $order = ['create_time desc'];
        $with = ['comments' => function ($query) use ($where, $order) {
            $commentsWhere = $where;
            unset($commentsWhere['pid']);
            $query->where($commentsWhere)->order($order)->with(['repleyuser', 'user'])->withcount(['comments']);
        }, 'user'];
        [$count, $list] = CommentModel::getCommentList($page, $limit, $where, $with, '*', $order);
        $data = ['page' => $page, 'list' => $list->toArray(), 'max_page' => ceil($count / $limit), 'next_page' => ++$page];;
        $this->success('', '', $data);
    }

    public function likeOrDislike()
    {
        $waitSign = md5('wait_time_likeordislike');
        $waitTime = Cookie::get($waitSign);
        $waitTimeResult = time() - $waitTime;
        if ($waitTimeResult <= 60) {
            $time = 60 - $waitTimeResult;
            $this->error('您点的也太频繁了，稍微缓缓。喝杯咖啡吧！' . $time . '秒后可重新提交');
        }
        $params = $this->request->param();
        $type = $params['type'];
        if ($type == 'article') {
            $model = new \app\common\model\Article();
        } elseif ($type == '') {
            $model = new \app\common\model\Article();
        }
        if ($params['action'] == 'like') {
            $result = $model->where(['id' => $params['id']])->inc('like', 1)->update();
        } elseif ($params['action'] == 'dislike') {
            $result = $model->where(['id' => $params['id']])->inc('dislike', 1)->update();
        }
        if ($result) {
            Cookie::set($waitSign, time());
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}
